<?php
	//数据库操作类支持读写分离
	class database
	{
		var	$write = array();
		var	$read = array();
		var $sql = array();
		var $count = 0;

		function database( $config )
		{
			$config['write']['link'] = null;
			$config['read']['link'] = null;
			$this->write = $config['write'];
			$this->read = $config['read'];
			register_shutdown_function( array( & $this, 'disconnect' ) );
		}

		function connect( $type = 'write' )
		{
			$result = true;

			if ( ! is_resource( $this->{$type}['link'] ) )
			{
				$this->{$type}['link'] = mysql_connect( $this->{$type}['host'], $this->{$type}['user'], $this->{$type}['pass'] );

				if ( ! is_resource( $this->{$type}['link'] ) )
				{
					$result = false;
				}
				else
				{
					mysql_select_db( $this->{$type}['name'], $this->{$type}['link'] );
					mysql_unbuffered_query( "set names 'utf8'", $this->{$type}['link'] );
				}
			}

			return $result;
		}

		function disconnect()
		{
			if ( is_resource( $this->write['link'] ) )
			{
				mysql_close( $this->write['link'] );
				$this->write['link'] = null;
			}

			if ( is_resource( $this->read['link'] ) )
			{
				mysql_close( $this->read['link'] );
				$this->read['link'] = null;
			}
		}

		function start()
		{
			$this->sql[$this->count] = array();
			$this->command( 'begin' );
		}

		function end( $command = '' )
		{
			$right = true;

			if ( isset( $this->sql[$this->count] ) )
			{
				foreach( $this->sql[$this->count] as $sql => $error )
				{
					if ( $error != '' )
					{
						$right = false;
						break;
					}
				}
			}

			$this->command( $command != '' ? $command : ( $right ? 'commit' : 'rollback' ) );
			$this->count++;
			return $right;
		}

		function command( $sql )
		{
			$result = 0;

			if ( $this->connect( 'write' ) )
			{
				mysql_unbuffered_query( $sql, $this->write['link'] );
				$this->sql[$this->count][$sql] = mysql_error( $this->write['link'] );
				$affected = $this->sql[$this->count][$sql] == '' ? mysql_affected_rows( $this->write['link'] ) : -1;
				$result = $this->sql[$this->count][$sql] == '' ? ( $affected > 0 ? $affected : 0 ) : -1;
			}

			return $result;
		}

		function select( $sql )
		{
			$data = array();

			if ( $this->connect( 'read' ) )
			{
				$result = mysql_unbuffered_query( $sql, $this->read['link'] );
				$error = mysql_error( $this->read['link'] );
				$this->sql[$this->count][$sql] = $error;
				$data = array();
				if ( $error == '' ) while( $row = mysql_fetch_assoc( $result ) ) $data[] = $row;
				if ( is_resource( $result ) ) mysql_free_result( $result );
			}

			return $data;
		}

		function total( $sql )
		{
			$total = $this->select( 'select count( 1 ) as total from ( select 1 ' . $sql . ' ) t' );
			return isset( $total[0]['total'] ) ? $total[0]['total'] : 0;
		}

		function split( $count, $list, $size, $current )
		{
			$total = $this->total( $count );
			$page = ceil( $total / $size );
			$current = is_numeric( $current ) && $current >= 1 ? $current : 1;
			$current = $current > $page ? $page : $current;
			$offset = $current == 1 ? 0 : ( $current - 1 ) * $size;
			$offset = $offset < 1 ? 0 : $offset;
			$result = array( 'total' => $total, 'page' => $page, 'current' => $current, 'size' => $size, 'offset' => $offset, 'data' => array() );
			if ( $total > 0 ) $result['data'] = $this->select( $list . ' limit ' . $offset . ', ' . $size );
			return $result;
		}

		function id()
		{
			return mysql_insert_id( $this->write['link'] );
		}

		function unique( $sql )
		{
			$data = $this->select( $sql );
			return isset( $data[1] ) ? array() : ( isset( $data[0] ) ? $data[0] : array() );
		}

		function add( $table, $array, $id = true )
		{
			$result = $this->command( 'insert into ' . $table . ' ( `' . join( '`, `', array_keys( $array ) ) . "` ) values ( '" . join( "', '", $array ) . "' )" );
			return $id ? $this->id() : $result;
		}

		function set( $table, $array, $term )
		{
			$sql = '';
			foreach( $array	as $key => $value )	$sql .=	"`" . $key . "` = '" . $value . "', ";
			$sql = 'update ' . $table . ' set ' . substr( $sql, 0, -2 ) . ' where ' . $this->term( $term );
			return $this->command( $sql );
		}

		function del( $table, $term )
		{
			return $this->command( 'delete from ' . $table . ' where ' . $this->term( $term ) );
		}

		function term( $array, $join = 'and' )
		{
			$sql = '';
			foreach( $array as $key => $value ) $sql .= preg_match( '!^\d+$!', $key ) ? ( ' ( ' . $value . ' ) ' . $join . ' ' ) : $key . " = '" . $value . "' " . $join . " ";
			return $sql == '' ? '' : ' ( ' . substr( $sql, 0, '-' . ( strlen( $join ) + 1 ) ) . ' ) ';
		}

		//执行纯sql,如use dbname命令
		function psql( $sql )
		{
			$result = 0;
			if ( $this->connect( 'write' ) )
			{
				$result = mysql_unbuffered_query( $sql, $this->write['link'] );
			}
			return $result;
		}

		//查主mysql，避免同步延时导致的问题
		function select_master( $sql )
		{
			$data = array();

			if ( $this->connect( 'write' ) )
			{
				$result = mysql_unbuffered_query( $sql, $this->write['link'] );
				$error = mysql_error( $this->write['link'] );
				$this->sql[$this->count][$sql] = $error;
				$data = array();
				if ( $error == '' ) while( $row = mysql_fetch_assoc( $result ) ) $data[] = $row;
				if ( is_resource( $result ) ) mysql_free_result( $result );
			}

			return $data;
		}

		function unique_master( $sql )
		{
			$data = $this->select_master( $sql );
			return isset( $data[1] ) ? array() : ( isset( $data[0] ) ? $data[0] : array() );
		}


	}
?>